export const isEmail = (str) => {
  // const reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
  const reg = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/
  return reg.test(str.toString())
}

export const isPhone = (str) => {
  const reg = /^(((13[0-9])|(14[579])|(15[0-3,5-9])|(16[56])|(17[013,5-8])|(18[0-9])|(19[89]))\d{8})$/
  return reg.test(str.toString())
}

// 获取 url 参数
export const getRequest = () => {
  const url = location.search // 获取url中"?"符后的字串
  const theRequest = {}
  if (url.indexOf('?') !== -1) {
    const str = url.substr(1)
    const strs = str.split('&')
    for (let i = 0; i < strs.length; i++) {
      theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1])
    }
  }
  return theRequest
}

// 过滤html标签
export const filterHtmlTag = (str) => {
  return str
    .replace(/(\n)/g, '')
    .replace(/(\t)/g, '')
    .replace(/(\r)/g, '')
    .replace(/<\/?[^>]*>/g, '')
    .replace(/\s*/g, '')
  // .replace(/&ldquo;/ig,'')
  // .replace(/&rdquo;/ig,'')
  // .replace(/&nbsp;/ig,'');
}

// 时间戳转时间
export const timestamp2Time = (timestamp) => {
  const date = new Date(timestamp * 1000) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const Y = date.getFullYear() + '-'
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  const D = date.getDate() + ' '
  // const h = date.getHours() + ':'
  // const m = date.getMinutes() + ':'
  // const s = date.getSeconds()
  // return Y + M + D + h + m + s;
  return Y + M + D
}

// 格式化日期
export const dateFormat = function (fmt, date = new Date()) {
  const o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'H+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (const k in o) { if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }
  return fmt
}

// 检测用户是否登录，若已登陆，返回该对象，若未登录，返回 null
export const checkUserData = (context) => {
  const data = JSON.parse(localStorage.getItem('userData'))
  if (context) {
    if (data) {
      return true
    } else {
      context.$alert('请登录！', '提示', {
        confirmButtonText: '确定',
        type: 'warning',
        lockScroll: false
      })
      return false
    }
  }
  return data
}
// 移除登录用户信息
// export const removeUserData = () => localStorage.removeItem('userData')

// 函数防抖
export const debounce = (method, delay = 100) => {
  let timer = null
  return function () {
    const context = this
    const args = arguments
    clearTimeout(timer)
    timer = setTimeout(function () {
      method.apply(context, args)
    }, delay)
  }
}

// 函数节流
export const throttle = (method, delay = 100) => {
  let timer = null
  return function () {
    const context = this
    const args = arguments
    timer = setTimeout(function () {
      clearTimeout(timer)
      timer = null
      method.apply(context, args)
    }, delay)
  }
}
